<?php


namespace app\zyhsweb\controller;

use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsRecode;
use think\Controller;
use think\Env;

/**
 * 新版星座解读
 * Class AstroExplainNew
 * @package app\web\controller
 */
class AstroExplainNew extends Controller
{

    protected $test = false;

    protected $LOG_PRE = '新版星座解读web错误：';

    /**
     * 获取新版星座解读数据
     * @return mixed
     * @throws \Exception
     */
    public function index()
    {
        if (!$this->request->isGet())
        {
            return show(config('code.error'), '未知操作');
        }

        $rid = input('rid', 0);

        if ($this->test === false)
        {
            //校验token
            try
            {
                $auth = new ZyhsAuth();
                $uid = (int)$auth->user['id'];
                if (0 === $rid)
                {//没传档案，那么取自己
                    $rid = (int)$auth->user['rid'];
                }
            }
            catch (\Exception $e)
            {
                throw new ApiException('星座解读，错误：权限认证失败,请稍后重试！');
            }
        } else
        {
            $uid = 1;
            $rid = 4923869;
        }

        if (!$rid)
        {
            return show(config('code.error'), '当前档案不存在');
        }
        $recode = (new ZyhsRecode())->getInfoById($rid);
        if ($recode['uid'] !== $uid || $recode['status'] !== 1)
        {
            return show(config('code.error'), '当前档案不存在');
        }
        //oss资源地址
        $oss = Env::get('host.photo');

        $astroExplainData = (new \app\web\service\AstroExplain())->getAstroExplain($recode);

        $smrData = $astroExplainData['smrData'];
        $data = $astroExplainData['data'];

        return $this->fetch('index_edition_for_two', ['smrData' => $smrData, 'oss' => $oss, 'data' => $data]);
    }


    /**
     * 获取新版星座解读数据（星座解读分享，android 11 以上，微博分享的是一个url，其余的 分享的是图片，和2.7.3 一起上）
     * @return mixed
     * @throws \Exception
     */
    public function share()
    {
        if (!$this->request->isGet())
        {
            return show(config('code.error'), '未知操作');
        }

        $token_param = input('token/s', '');
        $isEncode = input('is_encode/d', 0);
        if (!$isEncode)
        {
            $token = urldecode($token_param);
            //把空格替换成“+”
            $token = str_replace(' ', '+', $token);
        } elseif ($isEncode === 2)
        {
            $token = base64_decode($token_param, true);
        } else
        {
            $token = $token_param;
        }
        // access-token AES解密
        $str = (new Aes())->decryptWithOpenssl($token,'zyhsapp');
        if (empty($str))
        {
            throw new ApiException($this->LOG_PRE . '参数错误');
        }
        parse_str($str, $params);

        //检查输入参数是否正确
        if (!isset($params['uid']))
        {
            throw new ApiException($this->LOG_PRE . 'uid参数为空');
        }
        if (!isset($params['rid']))
        {
            throw new ApiException($this->LOG_PRE . 'rid参数为空');
        }

        $recode = (new ZyhsRecode())->getInfoById($params['rid']);
        if ($recode['uid'] !== $params['uid'] || $recode['status'] !== 1)
        {
            return show(config('code.error'), '当前档案不存在');
        }
        //oss资源地址
        $oss = Env::get('host.photo');

        $astroExplainData = (new \app\web\service\AstroExplain())->getAstroExplain($recode);

        $smrData = $astroExplainData['smrData'];
        $data = $astroExplainData['data'];
        return $this->fetch('index_edition_for_two', ['smrData' => $smrData, 'oss' => $oss, 'data' => $data]);
    }

    public function explainChatIntro()
    {
        $oss = Env::get('host.photo');

        return $this->fetch('', [
            'oss' => $oss,
        ]);
    }
}
